package com.hxh.generater.springBoot.springboot3_vue3.gen.crud;

import com.hxh.entity.Project;
import com.hxh.entity.ProjectDetail;
import com.hxh.entity.TemplateConfig;
import com.hxh.entity.po.ClassInfo;
import com.hxh.generater.common.remote.SpringbootRemoteUtil;
import com.hxh.generater.springBoot.springboot3_vue3.gen.GenSb3Vue3Util;
import com.hxh.util.GenUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.io.File;
import java.io.IOException;

/**
 * @ProjectName: AICode
 * @Package: com.hxh.generater
 * @Author: HXH
 * @Description:
 * @Date: 2023/11/28 15:08
 */
@Slf4j
public class GenMapperRemote {
    public static void generationMapper(Project project, ClassInfo classInfo) throws IOException {
        generationMapper(project, classInfo, true);
    }

    public static String generationMapper(String packageName, ClassInfo classInfo, TemplateConfig templateConfig) throws IOException {
        Project project = new Project();
        ProjectDetail backendProject = new ProjectDetail();
        backendProject.setProjectGroup(packageName);
        project.setBackendProject(backendProject);
        project.setTemplateConfig(templateConfig);
        return generationMapper(project, classInfo, false);
    }

    /**
     * @param project:
     * @param classInfo:
     * @return void
     * @author HXH
     * @description: 大部分依赖mybatis-plus，mapper只提供了基础的代码
     * @date 2024/5/22
     **/
    private static String generationMapper(Project project, ClassInfo classInfo, boolean writeFile) throws IOException {
        String className = classInfo.getClassName();
        String mapperName = className + "Mapper";

        SpringbootRemoteUtil springbootRemoteUtil = new SpringbootRemoteUtil();
        springbootRemoteUtil.setProject(project);
        springbootRemoteUtil.setClassInfo(classInfo);
        springbootRemoteUtil.setWriteFile(writeFile);

        springbootRemoteUtil.setWriteFilePath("mapper");
        String mutilDatasourcePathName = GenUtil.getMutilDatasourceStrByConfig(project, classInfo, "", File.separator);
        springbootRemoteUtil.setWriteFileName(mutilDatasourcePathName+mapperName);
        springbootRemoteUtil.setRemoteUrlSuffix("springboot/mapper");

        String projectNamePath = "";
        if (project != null) {
            projectNamePath = project.getName() + "\\";
        }
        String classNameEntity = StringUtils.uncapitalize(className);
        springbootRemoteUtil.setAdditionFilePath(GenSb3Vue3Util.FILE_PATH_PRE + "addition\\"+projectNamePath+"mapper\\" + classNameEntity);
        return springbootRemoteUtil.process();
    }


}
